local ClientOHOH = _G.ClientOHOH;
local M = ClientOHOH.class.new(script, ClientOHOH.files.BaseComponent());

local CommonOHOH = _G.CommonOHOH;
local ConnectClass=CommonOHOH.load.Connect();
local M = ClientOHOH.class.new(script, ClientOHOH.files.BaseComponent());

function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);
    self.ArriveEvent=ConnectClass.new()
    self.GoLostEvent=ConnectClass.new()
    self.Speed=50
    self.Arrive=10
end;

function M.dtor(self)
	if self.updateConnect then
        self.updateConnect:Disconnect()
        self.updateConnect=nil
    end
	M.super.dtor(self);
end;

function M.SetSpeed(self,speed)
    self.Speed=speed
end

function M.SetArriveCN(self,arrive)
    self.Arrive=arrive
end

function M.Update(self,dt)
    if self.PathIndex>#self.Path then
        self:Stop()
        self.ArriveEvent:Action()
    end
   local step= self.Speed*dt
    --当前点
   local nowPos=self.parent:GetPosition()
    -- 目标点
   local tagPos=self.Path[self.PathIndex]
   local d=tagPos-nowPos
   if d.Magnitude< self.Arrive then
        self.PathIndex=self.PathIndex+1
   end
   local nextPos=  nowPos+ d.Unit*step
   self.parent:MoveTo(nextPos)

end

function M.FollowUpdate(self,dt)
    if not self.followPart then
        self:Stop()
        self.GoLostEvent:Action()
    end
    local step= self.Speed*dt
    local nowPos=self.parent:GetPosition()
    local tagPos=self.followPart.Position
    local d=tagPos-nowPos
    if d.Magnitude< self.Arrive then
        self.ArriveEvent:Action()
    else
        local nextPos=  nowPos+ d.Unit*step
        self.parent:MoveTo(nextPos)
    end
end


function M.Stop(self)
	if self.updateConnect then
        self.updateConnect:Disconnect()
        self.updateConnect=nil
    end
end
--开始奔跑
function M.Go(self,path)
    if  self.updateConnect then
        return false
    end
    self.Path=path
    self.PathIndex=1
    self.updateConnect= self:AddStepUpdate(self.Update)
end


--跟随part
function  M.Follow(self,part)
    if  self.updateConnect then
        return false
    end
	self.followPart=part
    self.updateConnect= self:AddStepUpdate(self.FollowUpdate)
end



return M